/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.widget;

import sw6.digipecs.model.PECSBook;
import sw6.digipecs.widget.dragdrop.DragLayout;
import sw6.digipecs.widget.dragdrop.DraggableView;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.Animation;
/**
 * A Dragable Extended Image View, which is a view that can be dragged across the screen.
 * The view can be dragged the moment a touchEvent is 
 */
public class DragableExtImageView extends ExtImageView implements DraggableView {
	// Needed to start drag on the extImage
	private DragLayout mDragLayer;
	private boolean mBeingDragged = false;
	private PECSBook mBook;
	private int mPageDraggedFrom;
	private PageBarLayout mPageBar;
	private int mParentOffsetX;
	private int mParentOffsetY;
	
	/**
	 * Constructor
	 * @param context - the context to work within.
	 */
	public DragableExtImageView(Context context, DragLayout dl, PECSBook b, PageBarLayout pb) {
		super(context);
		
		mDragLayer = dl;
		mBook = b;
		mPageBar = pb;
	}
	
	public int getPageDraggedFrom() {
		return mPageDraggedFrom;
	}
	
	/**
	 * @see android.view.View#onTouchEvent(android.view.MotionEvent)
	 * @param event - the MotionEvent
	 * @return will return true if the event is MotionEvent.ACTION_DOWN
	 *  which will start dragging of the image. Otherwise we will let the
	 *  super class handle the touch event.
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		
		switch(action) {
		case MotionEvent.ACTION_DOWN:
			if (!getImage().isEmpty()) {
				play();
				if (mBook != null)
					mPageDraggedFrom = mBook.getPage();
				else
					mPageDraggedFrom = -1;
				final ViewGroup parent = ((ViewGroup) getParent()); 
				mParentOffsetX = parent.getLeft();
				mParentOffsetY = parent.getTop();
				mDragLayer.startDrag(this);
				return true;
			}
			break;
		}
		
		return super.onTouchEvent(event);
	}
	
	@Override
	public void setDragging(boolean b) {
		mBeingDragged = b;
	}
	
	@Override
	public boolean isDragging() {
		return mBeingDragged;
	}

	@Override
	public Animation getReturnAnimation(int fromX, int fromY, Bitmap bitmap) {
		Animation animation = null;
		
		// Checking prerequisites
		if (mPageBar == null) return null;
		if (mBook == null) return null;
		if (mBook.getPage() < 0) return null;
		
		if (mPageDraggedFrom != mBook.getPage()) {
			Log.i("sw6", "--- D ---");
			animation = mPageBar.createAnimationToPageBar(mPageDraggedFrom, fromX, fromY, bitmap.getWidth() / 2, bitmap.getHeight() / 2, null);
		}
			
		return animation;
	}
	
	public int getParentOffsetX() {
		return mParentOffsetX;
	}
	
	public int getParentOffsetY() {
		return mParentOffsetY;
	}
	
}
